package com.yunhe.inspect.task;

import com.yunhe.common.model.ResultObject;
import com.yunhe.inspect.service.DataInspectOrderService;
import com.yunhe.inspect.service.DataInspectPlanService;
import com.yunhe.inspect.service.RectificationSheetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

/**
 * @ClassName InspectOrderTask
 * @Description TODO
 * @Author HeTao
 * @Date 2021/11/11 9:03
 * @Version 1.0
 **/
@Api(value = "InspectOrderTask",description = "定时处理未处理的巡检工单",tags = "{InspectOrderTask_Task}")
@RestController
@RequestMapping(value = "/inspectOrderTask")
@Slf4j
public class InspectOrderTask {

    @Autowired
    private DataInspectOrderService dataInspectOrderService;

    @Autowired
    private RectificationSheetService rectificationSheetService;

    @ApiOperation("定时处理未处理的巡检工单")
    @RequestMapping(value ="/inspectPlanToOrder",method = RequestMethod.POST)
    @Scheduled(cron = "0 0 3 * * ? ")
    @Async
    public ResultObject resetSerialNumber() throws InterruptedException {
        log.info("定时处理未处理的巡检工单定时任务开始");
        //定时发布事项
        dataInspectOrderService.dealDataInspectOrder();
        log.info("定时处理未处理的巡检工单定时任务结束");
        return new ResultObject<>(null);
    }

    @ApiOperation("定时计算待巡检剩余天数和待复审剩余天数")
    @RequestMapping(value ="/calculateRemainingDays",method = RequestMethod.POST)
    @Scheduled(cron = "0 0 2 * * ?")
    @Async
    public ResultObject calculateRemainingDays() throws ParseException {
        //每天凌晨2点执行
        log.info("定时计算剩余天数定时任务开始");
        //定时计算剩余天数
        dataInspectOrderService.calculateRemainingDays();
        log.info("定时计算剩余天数定时任务结束");
        return new ResultObject<>(null);
    }

    @ApiOperation("更新整改单期限状态")
    @RequestMapping(value ="/setRectificationSheetDeadlineStatus",method = RequestMethod.POST)
    @Scheduled(cron = "0 5 0 * * ?")
    @Async
    public ResultObject setRectificationSheetDeadlineStatus() throws ParseException {
        //每天凌晨0点5分执行
        log.info("更新整改单期限状态任务开始");
        //定时更新整改单期限状态
        rectificationSheetService.setRectificationSheetDeadlineStatus();
        log.info("更新整改单期限状态任务结束");
        return new ResultObject<>(null);
    }
}
